<?php
    class ApiCenter_ProductApi_Helper_Data extends Mage_Core_Helper_Data
    {
        const STOREID="3";
    /**
    * 返回数组的维度
    * @param  [type] $arr [description]
    * @return [type]      [description]
    */
   public  function arrayLevel($arr){
       $al = array(0);
       $this-> aL($arr,$al);
       return max($al);
   }
   protected  function aL($arr,&$al,$level=0){
       if(is_array($arr)){
           $level++;
           $al[] = $level;
           foreach($arr as $v){
               $this->aL($v,$al,$level);
           }
       }
   }
   
   /**
    * 加载属性组分组的名称
    * */
   public function getAttributeGroupAction($attribute_group_id){
       $model = Mage::getSingleton('eav/entity_attribute_group');
       $res = $model->load($attribute_group_id)->getData();
       return $res['attribute_group_name'];
   }
   
   /**
    * 加载属性具有下拉的选项
    * */
   public function getAttributesOption($attributId) {
        
       $attributeObject=Mage::getModel('eav/config')->getAttribute('catalog_product',$code="")->load($attributId);
       $options = $attributeObject->setStoreId(self::STOREID)->getSource()->getAllOptions(false);
       array_unshift($options, array('label' => '请选择', 'value' => ''));
       return $options;
   }        
            
   /**
    * 还原wysiwyg 真实url链接
    * */
       public  function restoreMageImage($str){
           if(!is_string($str)){
               return "";
           }
           $realStr = $str;
           $path = Mage::getBaseUrl("media");
           try {
               $dealStr = preg_replace('/<img(.*?)src="({{media url="(.*?))"([^>]*)>/', '<img$1src="'.$path.'$3">', $str);
           } catch (Exception $e) {
               return $realStr;
           }
       
           // preg_match_all('/url="(.*)"/iUs', $imgstr, $out);         
         
            return $dealStr;
           
       }
       
       /**
        * 平凑成 wysiwyg 所要的格式
        * */
       public function togetherMageImage($str){
           if(!is_string($str)){
               return "";
           }
           $realStr = $str;
      
            
           //$system = "{{media url=";
           $path = Mage::getBaseUrl("media");
           $path = str_replace("/","\/",$path);
           $regular ='/<img(.*?)src="('.$path.'(.*?))"([^>]*)>/';
           try {
               $dealStr = preg_replace($regular,'<img$1src="{{media url="$3"}}"$4>',$str);
           } catch (Exception $e) {
               return $realStr;
           }           
           return $dealStr;
           
       }
    }
        
        
        
        